package com.ews.onlineexchange.controller;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.EntityGraph;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.*;
import com.ews.onlineexchange.service.UserService;
import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.controller.pub.QualificationConditionBean;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class BulletinList extends EntityQuery<Bulletin> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Inject
	QualificationConditionBean conditionBean;
	private ChooseType chooseType;
	private String projectName;
	private String buyerName;
	private String buyerId;
	private String tradeStatus;
	private String successSupplier;
	private String agentServiceId;
	public BulletinList(){
		setMaxResults(MAX_RESULTS_FEW);
		setOrder("createDate DESC");
	}

	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Bulletin> entity) {
		List<Predicate> restrictions = super.getRestrictions(cb, entity);
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		Join<Bulletin, ServiceBuyer> buyer = entity.join("buyer");
		//根据县区过滤
		super.addBulletinRegionRestriction_view(restrictions,buyer,cb);

		if(StringUtils.isNotBlank(getRegion())){
			restrictions.add(cb.equal(buyer.get("region"), getRegion()));
		}
		if (getChooseType() != null)
			restrictions.add(cb.equal(entity.get("wayOfChoose"), getChooseType()));
		if (StringUtils.isNotBlank(projectName))
			restrictions.add(cb.like(entity.get("projectName"), getStringRestriction(projectName)));
		if (StringUtils.isNotBlank(buyerName)) {
//			Join<Bulletin, ServiceBuyer> buyer = entity.join("buyer");
			restrictions.add(cb.like(buyer.get("cnname"), getStringRestriction(buyerName)));
		}
		Join<Object, Object> agentServices = null;
		if (StringUtils.isNotBlank( conditionBean.getAgentServiceId() ) ) {
			if(agentServices == null )agentServices = entity.join("qualificationGroup").join("groups").join("agentServices");
			restrictions.add( cb.equal( agentServices.get( "id" ), conditionBean.getAgentServiceId() ) );
		}
		
		if(StringUtils.isNoneBlank(tradeStatus)) {
			restrictions.add(cb.equal(entity.get("tradeStatus"), TradeStatus.valueOf(tradeStatus)));
		}
		if(StringUtils.isNoneBlank(successSupplier)) {
			System.out.println(getStringRestriction(successSupplier));
			Join<Trade,ServiceSupplier> successSupplier = entity.join("successTrade").join("supplier");
			restrictions.add(cb.like(successSupplier.get("cnname"), getStringRestriction(successSupplier)));
		}
		
		if (StringUtils.isNotBlank(buyerId)) {

			restrictions.add(cb.equal(buyer.get("id"), buyerId));
		}
		
		Join<Object, Object> qualificationTemplates = null;
		if (!StringUtils.isBlank(conditionBean.getQualificationClassId())) {
			if(qualificationTemplates == null )qualificationTemplates = entity.join("qualificationGroup").join("groups").join("qualificationTemplates");
			Join<QualificationTemplate, QualificationClass> clazz = qualificationTemplates.join("qualificationClass");
			restrictions.add(cb.equal(clazz.get("id"), conditionBean.getQualificationClassId()));
		}
		Join<QualificationClass, Sector> sector = null;
		if (!StringUtils.isBlank(conditionBean.getSectorId()) ) {
			Join<QualificationTemplate, QualificationClass> clazz = qualificationTemplates.join("qualificationClass");			
			sector = clazz.join("rootSectors");	
			restrictions.add(cb.equal(sector.get("id"), conditionBean.getSectorId()));
		}
		if(!StringUtils.isBlank(conditionBean.getChildSectorSno())) {
			sector = qualificationTemplates.join("sectors");	
			restrictions.add(cb.like(sector.get("sno"), getStringRestriction(conditionBean.getChildSectorSno())));
		}
		
		return restrictions;
	}
	
	
	public String getTradeStatus() {
		return tradeStatus;
	}

	public void setTradeStatus(String tradeStatus) {
		this.tradeStatus = tradeStatus;
	}
	
	@Override
	protected void setHint( TypedQuery<Bulletin> query ) {
		EntityGraph<?> entityGraph = entityManager.getEntityGraph( "Bulletin.QualificationGroup" );
		query.setHint( "javax.persistence.loadgraph", entityGraph );		
	}
	
	public String getSuccessSupplier() {
		return successSupplier;
	}

	public void setSuccessSupplier(String successSupplier) {
		this.successSupplier = successSupplier;
	}

	public String getAgentServiceId() {
		return agentServiceId;
	}

	public void setAgentServiceId(String agentServiceId) {
		this.agentServiceId = agentServiceId;
	}

	public String getBuyerId() {
		return buyerId;
	}

	public void setBuyerId(String buyerId) {
		this.buyerId = buyerId;
	}

	public ChooseType getChooseType() {
		return chooseType;
	}

	public void setChooseType(ChooseType chooseType) {
		this.chooseType = chooseType;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getBuyerName() {
		return buyerName;
	}

	public void setBuyerName(String buyerName) {
		this.buyerName = buyerName;
	}
	
}
